package com.slipper.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import jakarta.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.slipper.core.gson.GsonUtil;

public class ResultUtil {
    public static final String SUCCESS_CODE = "1";
    public static final String FAILURE_CODE = "0";
    /** 数据变量名 */
    private static final String DATA_NAME = "data";
    /** 数据返回编码 */
    private static final String CODE_NAME = "code";
    /** 数据返回消息内容 */
    private static final String MSG_NAME = "message";
    /** 页码 */
    private static final String PAGE_NUMBER_NAME = "pageNumber";
    /** 页面大小 */
    private static final String PAGE_SIZE_NAME = "pageSize";
    /** 总页数 */
    private static final String TOTAL_PAGE_NAME = "totalPage";
    /** 总记录数 */
    private static final String TOTAL_RECORD_NAME = "totalRecord";

    /**
     * 返回保存结果消息
     */
    public static String saveResult(Number result) {
        if (result != null && result.longValue() > 0) {
            return successResult("保存成功！");
        } else {
            return failureResult("保存失败！");
        }
    }

    /**
     * 返回保存结果消息
     */
    public static <T> String saveResult(Number result, T t) {
        if (result != null && result.longValue() > 0) {
            return successResult("保存成功！", t);
        } else {
            return failureResult("保存失败！");
        }
    }

    /**
     * @param result 受影响的行数
     * @param operate 操作类型
     */
    public static String operateResult(Number result, String operate) {
        if (result != null && result.longValue() > 0) {
            return successResult(operate + "成功！");
        } else {
            return failureResult(operate + "失败！");
        }
    }
    /**
     * 返回删除结果消息
     */
    public static String deleteResult(Number result) {
        if (result != null && result.longValue() > 0) {
            return successResult("删除成功！");
        } else {
            return failureResult("删除失败！");
        }
    }
    
    /**
     * 处理失败，返回错误消息
     */
    public static String failureResult() {
        return failureResult("操作失败！");
    }

    /**
     * 处理失败，返回错误消息
     */
    public static String failureResult(String message) {
        return failureResult(FAILURE_CODE, message);
    }

    /**
     * 处理失败，返回错误消息
     */
    public static String failureResult(String code, String message) {
        Map<String, Object> result = new HashMap<>();
        result.put(DATA_NAME, null);
        result.put(CODE_NAME, code);
        result.put(MSG_NAME, Tool.isBlank(message) ? "操作失败！" : message);
        return GsonUtil.toJson(result);
    }

    /**
     * 处理成功，返回消息
     */
    public static String successResult() {
        return successResult(null, null);
    }
    
    /**
     * 处理成功，返回消息
     */
    public static String successResult(String message) {
        return successResult(message, null);
    }

    /**
     * 处理成功，返回消息
     */
    public static String successDataResult(Object data) {
        return successResult(null, data);
    }

    /**
     * 处理成功，返回消息
     */
    public static String successResult(Object data) {
        return successResult(null, data);
    }

    /**
     * 处理成功，返回消息
     */
    public static String successResult(String message, Object data) {
        Map<String, Object> result = new HashMap<>();
        result.put(DATA_NAME, data);
        result.put(CODE_NAME, SUCCESS_CODE);
        result.put(MSG_NAME, Tool.isBlank(message) ? "操作成功！" : message);
        return GsonUtil.toJson(result);
    }

    /**
     * 分页返回信息
     */
    public static String successResult(PageInfo<?> pageInfo) {
        Map<String, Object> result = new HashMap<>();
        result.put(DATA_NAME, pageInfo.getList());
        result.put(PAGE_NUMBER_NAME, pageInfo.getPageNum());
        result.put(PAGE_SIZE_NAME, pageInfo.getPageSize());
        result.put(TOTAL_PAGE_NAME, pageInfo.getPages());
        result.put(TOTAL_RECORD_NAME, pageInfo.getTotal());
        result.put(CODE_NAME, SUCCESS_CODE);
        result.put(MSG_NAME, "查询成功。");
        return GsonUtil.toJson(result);
    }

    public static void failureResult(HttpServletResponse response, int code, String message) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Max-Age", "864000");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader(
                "Access-Control-Allow-Headers",
                "Access-Control-Allow-Headers, " +
                "Access-Control-Allow-Methods, " +
                "Access-Control-Allow-Origin, " +
                "Access-Control-Max-Age, " +
                "Authorization, " +
                "Cache-Control, " +
                "Content-Type"
        );
        response.setStatus(code);
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        writer.append(ResultUtil.failureResult(FAILURE_CODE, message));
        writer.close();
        response.flushBuffer();
    }
}
